﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class CRM_Manager_ERP_DemandPlanEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int c = BindCenterStore(Store_Rome, Request.QueryString["ptabid"]);
            if (c > 0)
                extcomb_StoreRoomId.SelectedIndex = 0;
            string objid = ViewState_ObjID = string.IsNullOrEmpty(Request.QueryString["ObjId"]) ? "Add" : Request.QueryString["ObjId"];
            ShowEditInfo(objid);
        }
    }

    private void ShowEditInfo(string objid)
    {
        if (objid == "Add")
        {
            exttxtf_CreaterName.Text = BS_LoginUserName;
            exthidn_Creater.Text = BS_LoginUserID;
            exttxtf_CreateTime.Text = DateTime.Now.ToDateTimeString();
        }
        else
        {
            var planDataTable = CRMMis.BLL.ERP_DemandPlan.GetList("DemandPlanID=" + objid).Tables[0];
            if (planDataTable.Rows.Count > 0)
            {
                DataRow row = planDataTable.Rows[0];
                int planState = Convert.ToInt32(row["PlanState"]);
                //根据状态控制
                if (planState != 1)
                    extBtn_CheckInfo.Hidden = true;
                if (planState > 0)
                {
                    extBtn_SubmitInfo.Hidden = true;
                    extBtn_Save.Hidden = true;
                    extBtn_AddItem.Hidden = extBtn_DeleteItem.Hidden = true;
                    SetContainerReadOnly(formPanel, true);
                    gridPanel_listdata.ColumnModel.Columns[1].Editable =                //商品名称
                        gridPanel_listdata.ColumnModel.Columns[6].Editable = false;     //数量
                }
                if (planState > 1)
                {
                    extBtn_CheckInfo.Hidden = true;
                }
            }
            SetContainer(formPanel, planDataTable);


            var planDetail = CRMMis.BLL.ERP_DemandDetail.GetList(" DPlanID=" + objid).Tables[0];
            Store1.DataSource = planDetail;
            Store1.DataBind();
        }
    }

    [DirectMethod]
    public void BindCargoComb(string searchText)
    {
        string sqWhere = "";
        if (!string.IsNullOrEmpty(searchText))
        {
            sqWhere = " CargoName like '%" + FilterSpecChar(searchText) + "%' or Spell like '%" + FilterSpecChar(searchText) + "%' ";
        }
        var datatable = CRMMis.BLL.ERP_Cargo.GetList(sqWhere, " CargoName asc");
        store_Cargo.DataSource = datatable;
        store_Cargo.DataBind();
    }

    /// <summary>
    /// 保存，
    /// </summary>
    /// <param name="jsonData"></param>
    /// <param name="type">0,保存;1,提交;2,审核</param>
    [DirectMethod]
    public void SaveData(string jsonData, int type)
    {
        List<CRMMis.Model.ERP_DemandDetail> details = JSON.Deserialize<List<CRMMis.Model.ERP_DemandDetail>>(jsonData);
        CRMMis.Model.ERP_DemandPlan planModel;
        if (ViewState_ObjID == "Add")
        {
            planModel = new CRMMis.Model.ERP_DemandPlan();
            planModel.DemandPlanID = CRMMis.BLL.ERP_DemandPlan.GetNewID();
            planModel.DemandPlanNo = GetBusinessNo("YH", "ERP_DemandPlanYH", Convert.ToInt32(extcomb_StoreRoomId.Value).ToString("000"));
            planModel.Creater = BS_LoginUserID;
            planModel.CreateTime = exttxtf_CreateTime.Text.Trim();
            planModel.PlanState = 0;
            if (type == 1)
                planModel.PlanState = 1;    //提交
        }
        else
        {
            planModel = CRMMis.BLL.ERP_DemandPlan.GetModel(Convert.ToInt32(ViewState_ObjID));
            if (type == 1)
            {
                if (planModel.PlanState == 0)
                    planModel.PlanState = 1;    //提交
                else
                {
                    X.Msg.Alert("系统提示", "提交失败！当前记录已经提交").Show();
                    return;
                }
            }
            else if (type == 2)
            {
                if (planModel.PlanState == 1)
                    planModel.PlanState = 2;    //审核
                else
                {
                    X.Msg.Alert("系统提示", "审核失败！当前记录已经审核").Show();
                    return;
                }
            }
        }
        if (type < 2)
        {
            planModel.DeptIndex = Convert.ToInt32(extcomb_StoreRoomId.Value);
            planModel.Remark = exttxtf_Remark.Text.Trim();
        }
        CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
        myTrans.BeginTransaction();
        try
        {
            if (ViewState_ObjID == "Add")
            {
                CRMMis.BLL.ERP_DemandPlan.Add(myTrans, planModel);
            }
            else
            {
                CRMMis.BLL.ERP_DemandPlan.Update(myTrans, planModel);
                //删除子表
                if (type < 2)
                    CRMMis.BLL.ERP_DemandDetail.DeleteFK(myTrans, "DPlanID = " + planModel.DemandPlanID);
            }
            if (type < 2)
            {
                foreach (CRMMis.Model.ERP_DemandDetail detailModel in details)
                {
                    detailModel.DemandDetailID = CRMMis.BLL.ERP_DemandDetail.GetNewID();
                    detailModel.DPlanID = planModel.DemandPlanID;
                    CRMMis.BLL.ERP_DemandDetail.Add(myTrans, detailModel);
                }
            }
            myTrans.CommitTran();
            ViewState_ObjID = planModel.DemandPlanID.ToString();
            exttxtf_DemandPlanNo.Text = planModel.DemandPlanNo;

            string handler = "";
            if (type > 0)
                handler = "closeCurrTab();";
            X.Msg.Alert("系统提示", "保存成功！", handler).Show();
        }
        catch (Exception ex)
        {
            myTrans.RollBackTran();
            X.Msg.Alert("系统提示", "保存失败！</br>" + ex.Message).Show();
        }
    }



    protected void OnExtBtn_ToExcel_Click(object sender, DirectEventArgs e)
    {
        var planDetail = CRMMis.BLL.ERP_DemandDetail.GetList(" DPlanID=" + ViewState_ObjID).Tables[0];
        string name = "要货单";
        CreateExcelOutput(name, name, planDetail, gridPanel_listdata, false, sheet1 =>
         {
             var row = sheet1.CreateRow(1);
             row.CreateCell(0).SetCellValue(exttxtf_DemandPlanNo.FieldLabel + "：" + exttxtf_DemandPlanNo.Text);
             sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 1));
             row.CreateCell(2).SetCellValue(extcomb_StoreRoomId.FieldLabel + "：" + extcomb_StoreRoomId.SelectedItem.Text);
             sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 2, 3));
             row.CreateCell(4).SetCellValue(exttxtf_CreaterName.FieldLabel + "：" + exttxtf_CreaterName.Text);
             sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 4, 6));

             var row2 = sheet1.CreateRow(2);
             row2.CreateCell(0).SetCellValue(exttxtf_CreateTime.FieldLabel + "：" + exttxtf_CreateTime.Text);
             sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 2, 0, 1));
             row2.CreateCell(2).SetCellValue(exttxtf_Remark.FieldLabel + "：" + exttxtf_Remark.Text);
             sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 2, 2, 6));

             int[] widths = new int[] { 20, 20, 20, 20, 15, 15, 15 };
             for (int i = 0; i < widths.Length; i++)
             {
                 sheet1.SetColumnWidth(i, widths[i] * 256);
             }
             return 2;
         });
    }

}